/*
 * ReceiverReportPanel.java
 *
 * Created on 29 czerwiec 2008, 01:58
 */
package pl.edu.agh.ssm.monitor.gui;

import java.util.LinkedList;
import java.util.List;
import pl.edu.agh.ssm.monitor.data.ReceiverReportInfo;
import pl.edu.agh.ssm.monitor.data.SessionConnection;
//import pl.edu.agh.ssm.monitor.utils.NTPUtils;

/**
 *
 * @author  aneezka
 */
@SuppressWarnings("serial")
public class ReceiverReportPanel extends javax.swing.JPanel implements UpdatePanel {

    /** Creates new form ReceiverReportPanel */
    public ReceiverReportPanel(SessionConnection connection) {
        this.connection = connection;
        initComponents();
        update();
    }

    @Override
    public String getName() {
        return "Receiver Report";
    }

    public void update() {
        ReceiverReportInfo report = connection.getReceiverReport();
        if (report != null) {
            jitterLabel.setText("" + report.getJitter());
            lostTotalLabel.setText("" + report.getPackagesLostTotal());
            seqNumLabel.setText("" + report.getLastPackageReceivedStamp());
            srDelayLabel.setText("" + report.getTimeFromLastSRRecv());
//            		+ " (" + NTPUtils.ntpToDate(report.getTimeFromLastSRRecv()) + ")");
            srTimestampLabel.setText("" + report.getLastSRRecvNTPTimeStamp()); 
//            		+ " (" + NTPUtils.ntpToDate(report.getLastSRRecvNTPTimeStamp()) + ")");
            fractionLabel.setText("" + (report.getPackagesLostFraction() * 100) + " [%]");
            for (UpdatePanelListener l : listeners) {
                l.panelUpdated(this);
            }
        } else {
            jitterLabel.setText(null);
            lostTotalLabel.setText(null);
            seqNumLabel.setText(null);
            srDelayLabel.setText(null);
            srTimestampLabel.setText(null);
            fractionLabel.setText(null);
            this.remove();
        }
    }

    public void remove() {
        for (UpdatePanelListener l : listeners) {
            l.panelRemoved(this);
        }
    }

    public void addListener(UpdatePanelListener listener) {
        listeners.add(listener);
    }

    public void removeListener(UpdatePanelListener listener) {
        listeners.remove(listener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jitterLabel = new javax.swing.JLabel();
        fractionLabel = new javax.swing.JLabel();
        lostTotalLabel = new javax.swing.JLabel();
        seqNumLabel = new javax.swing.JLabel();
        srTimestampLabel = new javax.swing.JLabel();
        srDelayLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pl.edu.agh.ssm.monitor.SessionMonitorApp.class).getContext().getResourceMap(ReceiverReportPanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jitterLabel.setText(resourceMap.getString("jitterLabel.text")); // NOI18N
        jitterLabel.setName("jitterLabel"); // NOI18N

        fractionLabel.setText(resourceMap.getString("fractionLabel.text")); // NOI18N
        fractionLabel.setName("fractionLabel"); // NOI18N

        lostTotalLabel.setText(resourceMap.getString("lostTotalLabel.text")); // NOI18N
        lostTotalLabel.setName("lostTotalLabel"); // NOI18N

        seqNumLabel.setText(resourceMap.getString("seqNumLabel.text")); // NOI18N
        seqNumLabel.setName("seqNumLabel"); // NOI18N

        srTimestampLabel.setText(resourceMap.getString("srTimestampLabel.text")); // NOI18N
        srTimestampLabel.setName("srTimestampLabel"); // NOI18N

        srDelayLabel.setText(resourceMap.getString("srDelayLabel.text")); // NOI18N
        srDelayLabel.setName("srDelayLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(srDelayLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addComponent(srTimestampLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addComponent(seqNumLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addComponent(lostTotalLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addComponent(fractionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addComponent(jitterLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jitterLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(fractionLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lostTotalLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(seqNumLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(srTimestampLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(srDelayLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    private SessionConnection connection;
    private List<UpdatePanelListener> listeners = new LinkedList<UpdatePanelListener>();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel fractionLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jitterLabel;
    private javax.swing.JLabel lostTotalLabel;
    private javax.swing.JLabel seqNumLabel;
    private javax.swing.JLabel srDelayLabel;
    private javax.swing.JLabel srTimestampLabel;
    // End of variables declaration//GEN-END:variables
}
